<template>
  <div>
    <el-dialog :visible="showDialog" title="新增图书信息" width="30%">
      <el-form :model="bookForm" :rules="rules" ref="bookForm" label-width="120px">
        <el-form-item label="书名" prop="name">
          <el-input v-model="bookForm.name"></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <el-input v-model="bookForm.cover"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="bookForm.author"></el-input>
        </el-form-item>
        <el-form-item label="首版日期" prop="firstEditionDate">
          <el-date-picker
            v-model="bookForm.firstEditionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-select v-model="bookForm.category">
            <el-option label="散文" value="散文"></el-option>
            <el-option label="社会纪实" value="社会纪实"></el-option>
            <el-option label="小说" value="小说"></el-option>
            <el-option label="文学" value="文学"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="submitForm">提交</el-button>
          <el-button @click="changeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    showDialog: Boolean
  },
  data () {
    return {
      loading: false,
      bookForm: {
        name: '',
        cover: '',
        author: '',
        firstEditionDate: '',
        category: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入书名', trigger: 'blur' },
          { min: 1, max: 100, message: '书名长度应在 1 到 100 个字符之间', trigger: 'blur' }
        ],
        cover: [
          { required: true, message: '请输入封面', trigger: 'blur' },
          { min: 1, max: 200, message: '封面长度应在 1 到 200 个字符之间', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '请输入作者', trigger: 'blur' },
          { min: 1, max: 100, message: '作者长度应在 1 到 100 个字符之间', trigger: 'blur' }
        ],
        firstEditionDate: [
          { required: true, message: '请选择首版日期', trigger: 'change' }
        ],
        category: [
          { required: true, message: '请选择类别', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.loading = true
      this.$refs.bookForm.validate((valid) => {
        if (valid) {
          console.log(this.bookForm)
          this.$store.dispatch('Book/postBook', this.bookForm)
            .then(() => {
              this.loading = false
              this.$message.success('新增成功')
              this.$emit('changeDialog', false)
            })
            .catch((error) => {
              this.loading = false
              console.error(error)
            })
        } else {
          this.loading = false
          console.log('表单验证不通过')
        }
      })
    },
    changeDialog () {
      this.$emit('changeDialog', false)
    }
  }
}
</script>

<style>

</style>
